package com.CityHunter.Request;


import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;

import android.util.Log;

import com.CityHunter.Common.Http;
import com.CityHunter.DataObject.Group;

public class GetGroupRequest {

	private String getGroupPage;
	
	public GetGroupRequest(String getGroupPage){
		this.getGroupPage = getGroupPage;
	}
	
	public ArrayList<Group> sendRequest(int userid){

		ArrayList<Group> groups;
		
		// Declare NameValuePair to store the data
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("userid", Integer.toString(userid)));

		// Send Get Buddy Request
		Http http = new Http();
		groups = new ArrayList<Group>();
		Log.d("bdyp_Debug", "Sending Request : " + getGroupPage);
		JSONArray jsonResponse = http.sendRequest(getGroupPage, params);
		try {
			for (int i = 0; i < jsonResponse.length(); i++) {

				int groupid = Integer.parseInt(jsonResponse.getJSONObject(i)
						.getString("groupid"));
				String title = jsonResponse.getJSONObject(i).getString(
						"groupTitle");
				String description = jsonResponse.getJSONObject(i).getString(
						"description");

				Group gb = new Group(groupid, title, description);
				groups.add(gb);
			}
			Log.d("bdyp_Debug",
					"Response : No of Group = " + jsonResponse.length());
		} catch (Exception e) {
			Log.e("bdyp_Error", e.getMessage().toString());
		}
		
		return groups;
	}
	
}
